<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>IO | 浪飞IT小栈</title>
    <meta name="generator" content="VuePress 1.9.9">
    <link rel="icon" href="/img/favicon.ico">
    <meta name="description" content="前后端技术分享">
    <meta name="keywords" content="前端博客,后端博客,人工智能,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown,java,Spring,Springboot,Redis,mysql">
    <meta name="baidu-site-verification" content="7F55weZDDc">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/assets/css/0.styles.ada71c49.css" as="style"><link rel="preload" href="/assets/js/app.a2a00aa5.js" as="script"><link rel="preload" href="/assets/js/2.256f807a.js" as="script"><link rel="preload" href="/assets/js/27.1eb62850.js" as="script"><link rel="prefetch" href="/assets/js/10.ab144fe3.js"><link rel="prefetch" href="/assets/js/11.062e0879.js"><link rel="prefetch" href="/assets/js/12.f9c60882.js"><link rel="prefetch" href="/assets/js/13.0a6ebfcf.js"><link rel="prefetch" href="/assets/js/14.03fc6f12.js"><link rel="prefetch" href="/assets/js/15.19dbf9fd.js"><link rel="prefetch" href="/assets/js/16.6e68160b.js"><link rel="prefetch" href="/assets/js/17.1d678834.js"><link rel="prefetch" href="/assets/js/18.4536fd5c.js"><link rel="prefetch" href="/assets/js/19.9c1b65df.js"><link rel="prefetch" href="/assets/js/20.c5f96e71.js"><link rel="prefetch" href="/assets/js/21.3f0b334e.js"><link rel="prefetch" href="/assets/js/22.55c868be.js"><link rel="prefetch" href="/assets/js/23.91286241.js"><link rel="prefetch" href="/assets/js/24.c1af1909.js"><link rel="prefetch" href="/assets/js/25.a995a755.js"><link rel="prefetch" href="/assets/js/26.abc5dea3.js"><link rel="prefetch" href="/assets/js/28.b383a871.js"><link rel="prefetch" href="/assets/js/29.088e0d73.js"><link rel="prefetch" href="/assets/js/3.cd716564.js"><link rel="prefetch" href="/assets/js/30.3ccc98d5.js"><link rel="prefetch" href="/assets/js/31.aab7cc23.js"><link rel="prefetch" href="/assets/js/32.5e16b7e6.js"><link rel="prefetch" href="/assets/js/33.4aaeb741.js"><link rel="prefetch" href="/assets/js/34.45e8e898.js"><link rel="prefetch" href="/assets/js/35.5899c97a.js"><link rel="prefetch" href="/assets/js/36.32e2e263.js"><link rel="prefetch" href="/assets/js/37.c4e27bbd.js"><link rel="prefetch" href="/assets/js/38.fe5e60d2.js"><link rel="prefetch" href="/assets/js/39.2cb0b4ee.js"><link rel="prefetch" href="/assets/js/4.edf4d5e7.js"><link rel="prefetch" href="/assets/js/40.08d5bead.js"><link rel="prefetch" href="/assets/js/41.2f80f24b.js"><link rel="prefetch" href="/assets/js/42.43a49913.js"><link rel="prefetch" href="/assets/js/43.08da7ec2.js"><link rel="prefetch" href="/assets/js/44.7c7cb8a9.js"><link rel="prefetch" href="/assets/js/45.0ddf7e7f.js"><link rel="prefetch" href="/assets/js/46.7c4609a5.js"><link rel="prefetch" href="/assets/js/47.edf3ddf8.js"><link rel="prefetch" href="/assets/js/48.569a90b9.js"><link rel="prefetch" href="/assets/js/49.fa368ac8.js"><link rel="prefetch" href="/assets/js/5.24054156.js"><link rel="prefetch" href="/assets/js/50.b9b3ea7f.js"><link rel="prefetch" href="/assets/js/51.a2ebf0fe.js"><link rel="prefetch" href="/assets/js/52.31f02238.js"><link rel="prefetch" href="/assets/js/53.4110ef1e.js"><link rel="prefetch" href="/assets/js/54.dab741c7.js"><link rel="prefetch" href="/assets/js/55.f27984e9.js"><link rel="prefetch" href="/assets/js/56.bd4c1a8e.js"><link rel="prefetch" href="/assets/js/57.375d40cb.js"><link rel="prefetch" href="/assets/js/58.932e3864.js"><link rel="prefetch" href="/assets/js/59.e657be1b.js"><link rel="prefetch" href="/assets/js/6.b259c061.js"><link rel="prefetch" href="/assets/js/60.88d94274.js"><link rel="prefetch" href="/assets/js/61.fa71d884.js"><link rel="prefetch" href="/assets/js/62.a9d3b3b7.js"><link rel="prefetch" href="/assets/js/63.f849fe75.js"><link rel="prefetch" href="/assets/js/64.1770f7a9.js"><link rel="prefetch" href="/assets/js/65.6afc33db.js"><link rel="prefetch" href="/assets/js/66.799f2eb4.js"><link rel="prefetch" href="/assets/js/67.f36b2f7d.js"><link rel="prefetch" href="/assets/js/68.868cd00b.js"><link rel="prefetch" href="/assets/js/69.4c29b907.js"><link rel="prefetch" href="/assets/js/7.e34ea00f.js"><link rel="prefetch" href="/assets/js/70.1b993ed4.js"><link rel="prefetch" href="/assets/js/71.e16ad4f9.js"><link rel="prefetch" href="/assets/js/72.627505e6.js"><link rel="prefetch" href="/assets/js/73.32768e14.js"><link rel="prefetch" href="/assets/js/74.968a580d.js"><link rel="prefetch" href="/assets/js/75.42d5af3d.js"><link rel="prefetch" href="/assets/js/76.19a69d31.js"><link rel="prefetch" href="/assets/js/77.cf627a78.js"><link rel="prefetch" href="/assets/js/78.07177880.js"><link rel="prefetch" href="/assets/js/8.c4dd00ce.js"><link rel="prefetch" href="/assets/js/9.0ff73be2.js">
    <link rel="stylesheet" href="/assets/css/0.styles.ada71c49.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/logo.png" alt="浪飞IT小栈" class="logo"> <span class="site-name can-hide">浪飞IT小栈</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/avatar.jpg"> <div class="blogger-info"><h3>浪飞yes</h3> <span>无法简单的人儿~</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>开篇</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础语法</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>面向对象</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>常用类</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>基础高级</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/0c7696/" class="sidebar-link">集合框架</a></li><li><a href="/pages/19e70b/" class="sidebar-link">异常机制</a></li><li><a href="/pages/a53030/" class="sidebar-link">多线程</a></li><li><a href="/pages/16b819/" aria-current="page" class="active sidebar-link">IO</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/pages/16b819/#一、今日学习内容和目标" class="sidebar-link">一、今日学习内容和目标</a></li><li class="sidebar-sub-header level2"><a href="/pages/16b819/#二、io入门" class="sidebar-link">二、IO入门</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/16b819/#_2-1-file类-掌握" class="sidebar-link">2.1 File类（掌握）</a></li><li class="sidebar-sub-header level3"><a href="/pages/16b819/#_2-2-字符编码" class="sidebar-link">2.2 字符编码</a></li><li class="sidebar-sub-header level4"><a href="/pages/16b819/#_2-2-1-编码表介绍-了解" class="sidebar-link">2.2.1 编码表介绍 ( 了解 )</a></li><li class="sidebar-sub-header level6"><a href="/pages/16b819/#简单理解" class="sidebar-link">简单理解：</a></li><li class="sidebar-sub-header level6"><a href="/pages/16b819/#ascii码表" class="sidebar-link">ASCII码表：</a></li><li class="sidebar-sub-header level6"><a href="/pages/16b819/#gb2312-gbk-gb18030" class="sidebar-link">GB2312/GBK/GB18030：</a></li><li class="sidebar-sub-header level6"><a href="/pages/16b819/#unicode码表" class="sidebar-link">Unicode码表：</a></li><li class="sidebar-sub-header level4"><a href="/pages/16b819/#_2-2-2-字符串的编码和解码操作-知道" class="sidebar-link">2.2.2 字符串的编码和解码操作（知道）</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/16b819/#三、io流介绍" class="sidebar-link">三、IO流介绍</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/16b819/#_3-1-为什么要学习io流" class="sidebar-link">3.1 为什么要学习IO流</a></li><li class="sidebar-sub-header level3"><a href="/pages/16b819/#_3-2-io操作示意图" class="sidebar-link">3.2 IO操作示意图</a></li><li class="sidebar-sub-header level3"><a href="/pages/16b819/#_3-3-流的分类-掌握" class="sidebar-link">3.3 流的分类（掌握）</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/16b819/#四、字节流" class="sidebar-link">四、字节流</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/16b819/#_4-1-文件字节输出流-重点" class="sidebar-link">4.1 文件字节输出流 ( 重点 )</a></li><li class="sidebar-sub-header level4"><a href="/pages/16b819/#_4-1-1-文件字节输出流介绍" class="sidebar-link">4.1.1 文件字节输出流介绍</a></li><li class="sidebar-sub-header level6"><a href="/pages/16b819/#fileoutputstream类" class="sidebar-link">FileOutputStream类 :</a></li><li class="sidebar-sub-header level4"><a href="/pages/16b819/#_4-1-2-fileoutputstream-成员" class="sidebar-link">4.1.2 FileOutputStream 成员</a></li><li class="sidebar-sub-header level3"><a href="/pages/16b819/#_4-2、文件字节输入流-重点" class="sidebar-link">4.2、文件字节输入流 ( 重点 )</a></li><li class="sidebar-sub-header level4"><a href="/pages/16b819/#_4-2-1-字节输入流介绍" class="sidebar-link">4.2.1 字节输入流介绍</a></li><li class="sidebar-sub-header level4"><a href="/pages/16b819/#_4-2-2-fileinputstream-类成员" class="sidebar-link">4.2.2 FileInputStream 类成员</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/16b819/#五、字符流" class="sidebar-link">五、字符流</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/16b819/#_5-1-文件字符输出流" class="sidebar-link">5.1 文件字符输出流</a></li><li class="sidebar-sub-header level4"><a href="/pages/16b819/#_5-1-1-字符输出流介绍" class="sidebar-link">5.1.1 字符输出流介绍</a></li><li class="sidebar-sub-header level6"><a href="/pages/16b819/#filewriter类" class="sidebar-link">FileWriter类 :</a></li><li class="sidebar-sub-header level4"><a href="/pages/16b819/#_5-1-2-filewriter-成员" class="sidebar-link">5.1.2 FileWriter 成员</a></li><li class="sidebar-sub-header level3"><a href="/pages/16b819/#_5-2-文件字符输入流" class="sidebar-link">5.2 文件字符输入流</a></li><li class="sidebar-sub-header level4"><a href="/pages/16b819/#_5-2-1-字符输入流介绍" class="sidebar-link">5.2.1 字符输入流介绍</a></li><li class="sidebar-sub-header level4"><a href="/pages/16b819/#_5-2-2-filereader的成员" class="sidebar-link">5.2.2  FileReader的成员</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/16b819/#六、包装流" class="sidebar-link">六、包装流</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/16b819/#_6-1-字节缓冲流" class="sidebar-link">6.1 字节缓冲流</a></li><li class="sidebar-sub-header level3"><a href="/pages/16b819/#_6-2-字符缓冲流" class="sidebar-link">6.2 字符缓冲流</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/16b819/#七、小结与作业" class="sidebar-link">七、小结与作业</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/16b819/#_7-1-小结" class="sidebar-link">7.1 小结</a></li><li class="sidebar-sub-header level3"><a href="/pages/16b819/#_7-2-作业" class="sidebar-link">7.2 作业</a></li><li class="sidebar-sub-header level4"><a href="/pages/16b819/#_7-2-1-题目1" class="sidebar-link">7.2.1 题目1</a></li><li class="sidebar-sub-header level4"><a href="/pages/16b819/#_7-2-2-题目2" class="sidebar-link">7.2.2 题目2</a></li><li class="sidebar-sub-header level4"><a href="/pages/16b819/#_7-2-3-题目3" class="sidebar-link">7.2.3 题目3</a></li><li class="sidebar-sub-header level4"><a href="/pages/16b819/#_7-2-4-题目4" class="sidebar-link">7.2.4 题目4</a></li><li class="sidebar-sub-header level4"><a href="/pages/16b819/#_7-2-5-题目5" class="sidebar-link">7.2.5 题目5</a></li><li class="sidebar-sub-header level4"><a href="/pages/16b819/#_7-2-6-题目6" class="sidebar-link">7.2.6 题目6</a></li><li class="sidebar-sub-header level4"><a href="/pages/16b819/#_7-2-7-题目7" class="sidebar-link">7.2.7 题目7</a></li><li class="sidebar-sub-header level4"><a href="/pages/16b819/#_7-2-8-题目8" class="sidebar-link">7.2.8 题目8</a></li></ul></li></ul></li><li><a href="/pages/258730/" class="sidebar-link">单元测试与配置文件</a></li><li><a href="/pages/1fdc1c/" class="sidebar-link">反射与内省机制</a></li><li><a href="/pages/80d6e3/" class="sidebar-link">Lambda表达式</a></li><li><a href="/pages/a53dcc/" class="sidebar-link">Stream流操作</a></li><li><a href="/pages/42948c/" class="sidebar-link">注解</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前端技术</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>热门框架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前后端分离</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>权限控制</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>NoSQL</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>微服务</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>消息中间件</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>脚手架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>技术增值</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/categories/?category=Java%E9%80%9F%E9%80%9A" title="分类" data-v-06225672>Java速通</a></li><li data-v-06225672><a href="/categories/?category=%E5%9F%BA%E7%A1%80%E9%AB%98%E7%BA%A7" title="分类" data-v-06225672>基础高级</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://github.com/langfeiyes" target="_blank" title="作者" class="beLink" data-v-06225672>langfeiyes</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2024-03-03</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">IO<!----></h1>  <div class="theme-vdoing-content content__default"><h1 id="io流"><a href="#io流" class="header-anchor">#</a> IO流</h1> <h2 id="一、今日学习内容和目标"><a href="#一、今日学习内容和目标" class="header-anchor">#</a> 一、今日学习内容和目标</h2> <ul><li>掌握File类和基本操作</li> <li>知道字符编码和解码
<ul><li>常见的字符集，各自有什么特点</li> <li>字符的编码和解码操作</li></ul></li> <li>了解什么是IO，IO有哪些分类</li> <li>掌握四个文件流的操作
<ul><li>熟练掌握使用文件字节流读写文件</li> <li>熟练掌握使用文件字符流读写文件</li></ul></li> <li>掌握文件拷贝操作</li> <li>包装流
<ul><li>掌握字节缓冲流</li> <li>掌握字符缓冲流</li></ul></li></ul> <h2 id="二、io入门"><a href="#二、io入门" class="header-anchor">#</a> 二、IO入门</h2> <h3 id="_2-1-file类-掌握"><a href="#_2-1-file类-掌握" class="header-anchor">#</a> 2.1 File类（掌握）</h3> <p>File可以理解为文件和文件夹（目录），用于表示磁盘中某个文件或文件夹的路径。主要用于文件和目录的创建、查询和删除等操作。</p> <p><strong>注意：</strong></p> <ul><li>只能获取和设置文件本身的信息（文件大小，是否可读），<strong>不能</strong>设置和获取文件里面的内容。</li> <li>路径分隔符
<ul><li>Unix/Linux/Mac:  使用<code>/</code>来表示路径分隔符。</li> <li>Windows: 默认情况下是不区分大小写的，使用<code>\</code>来分割目录路径。但是在Java中一个<code>\</code>表示转义，所以在Windows系统中就得使用两个<code>\\</code>。</li></ul></li></ul> <p><strong>构造器</strong></p> <ul><li>File (String pathname)  根据给定的路径名来创建一个File对象</li></ul> <p><strong>常用方法</strong> （请使用ChatGPT完成）</p> <p>File类的创建功能</p> <ul><li>boolean createNewFile()     : 创建一个新的空的文件</li> <li>boolean mkdirs()：创建当前目录和上级目录</li></ul> <p>File类的删除功能</p> <ul><li>boolean delete() ：删除文件或者目录</li></ul> <p>File类的获取功能</p> <ul><li>String getName()：获取文件名称</li> <li>String getPath()：获取文件的路径</li> <li>File getParentFile()：获取上级目录</li> <li>String getAbsolutePath()：获取绝对路径</li></ul> <p>File类的判断功能</p> <ul><li>boolean exists()：判断文件或者目录是否存在</li> <li>boolean isFile() ：是否是文件</li> <li>boolean isDirectory()：判断是否是目录</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">FileDemo</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
		<span class="token class-name">File</span> f <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span><span class="token string">&quot;C:/test/123.txt&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>f<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//123.txt</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>f<span class="token punctuation">.</span><span class="token function">getPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//C:/test/123.txt</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>f<span class="token punctuation">.</span><span class="token function">getAbsolutePath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//C:/test/123.txt</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>f<span class="token punctuation">.</span><span class="token function">getParentFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//test</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>f<span class="token punctuation">.</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//true</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>f<span class="token punctuation">.</span><span class="token function">isFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//true</span>
		<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>f<span class="token punctuation">.</span><span class="token function">isDirectory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//false</span>
		
		<span class="token comment">//如果当前文件的父文件夹不存在,则创建</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>f<span class="token punctuation">.</span><span class="token function">getParentFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
			f<span class="token punctuation">.</span><span class="token function">getParentFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">mkdirs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><h3 id="_2-2-字符编码"><a href="#_2-2-字符编码" class="header-anchor">#</a> 2.2 字符编码</h3> <h4 id="_2-2-1-编码表介绍-了解"><a href="#_2-2-1-编码表介绍-了解" class="header-anchor">#</a> 2.2.1 编码表介绍 ( 了解 )</h4> <ul><li><p>计算机中储存的信息都是用二进制数据表示的；我们在屏幕上看到的英文、汉字等字符是二进制数转换之后的结果</p></li> <li><p>按照编码表规则，将字符存储到计算机中，称为编码。</p></li> <li><p>按照同样的编码表规则，将存储在计算机中的二进制数据解析显示出来，称为解码 。</p></li> <li><h6 id="简单理解"><a href="#简单理解" class="header-anchor">#</a> 简单理解：</h6> <ul><li>存储一个字符a，首先需在码表中查到对应的数字是97，然后按照转换成二进制的规则进行存储，称为编码</li> <li>读取的时候，先把二进制解析出来，再转成97，通过97查找编码表中对应的字符是a，称为解码</li></ul></li> <li><h6 id="ascii码表"><a href="#ascii码表" class="header-anchor">#</a> ASCII码表：</h6> <ul><li>包括了数字字符，英文大小写字符和一些常见的标点符号字符。</li> <li>注意：ASCII码表中是没有中文的。</li></ul></li> <li><h6 id="gb2312-gbk-gb18030"><a href="#gb2312-gbk-gb18030" class="header-anchor">#</a> GB2312/<strong>GBK</strong>/GB18030：</h6> <ul><li>win7系统默认的码表。兼容ASCII码表，也包含了21003个汉字，并支持简、繁体汉字</li> <li>注意：<strong>GBK</strong>是中国的码表，一个中文以两个字节的形式存储。但不包含世界上其他国家的文字。</li></ul></li> <li><h6 id="unicode码表"><a href="#unicode码表" class="header-anchor">#</a> Unicode码表：</h6> <ul><li>由国际组织ISO 制定，是统一的万国码表，计算机科学领域里的一项业界标准，容纳全球多数国家的常见文字和符号。</li> <li>注意： Unicode是万国码表标准，以UTF-8实现后一个中文以三个字节的形式存储</li></ul></li></ul> <p><strong>存储字母、数字、汉字的常识 （记住）</strong> ：</p> <p>（1）存储字母和数字无论是什么字符集都占1个字节.</p> <p>（2）存储汉字，<strong>GBK家族占两个字节</strong>，<strong>UTF-8家族占3个字节。</strong></p> <p>注意：不能使用单字节的字符集（ASCII）来存储中文，否则会乱码。</p> <h4 id="_2-2-2-字符串的编码和解码操作-知道"><a href="#_2-2-2-字符串的编码和解码操作-知道" class="header-anchor">#</a> 2.2.2 字符串的编码和解码操作（知道）</h4> <ul><li>编码：把字符 按照编码表 转成二进制数据
<ul><li>byte[] string.getBytes(&quot;UTF-8&quot;)</li></ul></li> <li>解码：把二进制数据 按照编码表 转成字符
<ul><li>new String(bytes, &quot;UTF-8&quot;)</li></ul></li></ul> <p><strong>注意：编码和解码使用的码表必须一致，否则会导致乱码。</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">EncodeDemo</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">UnsupportedEncodingException</span> <span class="token punctuation">{</span>
        <span class="token comment">// 字符编码: 把字符串 按照指定的字符集转成 二进制序列(byte数组) =&gt; getBytes()</span>
        <span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">&quot;abc中&quot;</span><span class="token punctuation">;</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> bytes <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token string">&quot;UTF-8&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>bytes<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 字符解码： 把二进制序列(byte[]) 按照指定的字符集转成字符</span>
        <span class="token class-name">String</span> str2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>bytes<span class="token punctuation">,</span> <span class="token string">&quot;GBK&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;str2 = &quot;</span> <span class="token operator">+</span> str2<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>拓展：String str = new String(bytes,0,3,&quot;UTF-8&quot;);</p> <h2 id="三、io流介绍"><a href="#三、io流介绍" class="header-anchor">#</a> 三、IO流介绍</h2> <h3 id="_3-1-为什么要学习io流"><a href="#_3-1-为什么要学习io流" class="header-anchor">#</a> 3.1 为什么要学习IO流</h3> <ul><li>通过变量,数组,或者集合存储数据
<ul><li>都是不能永久化存储 , 因为数据都是存储在内存中</li> <li>只要代码运行结束，所有数据都会丢失</li></ul></li> <li>使用IO流
<ul><li>将数据写到文件中，实现数据永久化存储</li> <li>把文件中的数据读取到内存中(Java程序)</li></ul></li></ul> <h3 id="_3-2-io操作示意图"><a href="#_3-2-io操作示意图" class="header-anchor">#</a> 3.2 IO操作示意图</h3> <ul><li>I  表示input  ，是数据从硬盘进<strong>内存</strong>的过程，称之为读。</li> <li>O 表示output ，是数据从<strong>内存</strong>到硬盘的过程。称之为写</li> <li>IO的数据传输，可以看做是一种数据的流动，按照流动的方向，以内存为参照物，进行读写操作</li></ul> <p>讲解IO知识点的时候，习惯和生活中的水流联系起来，一起来看看复古的水井和水缸。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub//io-principle.png" alt=""></strong></p> <p>此时站在水缸的角度，分析IO的操作方向：</p> <p>输入操作：水井——&gt;水缸</p> <p>输出操作：水缸——&gt;饭锅</p> <p><strong>也就是说：谁拥有数据，谁就是源，把数据流到哪里，哪里就是目的地。那么，请问水缸是源还是目标？</strong></p> <p>我们一般**站在程序( 内存 )**的角度：</p> <ul><li><p>程序需要数据 → 把数据读进来 →	输入操作（read）：读进来</p></li> <li><p>程序保存数据 → 把数据写出去 →    输出操作（write）：写出去</p></li></ul> <p><strong>六字心决：读进来，写出去（仔细揣摩这六个字有什么高深的含义）</strong></p> <h3 id="_3-3-流的分类-掌握"><a href="#_3-3-流的分类-掌握" class="header-anchor">#</a> 3.3 流的分类（掌握）</h3> <p>根据流的不同特性，流的划分是不一样的，一般按照如下情况来考虑：</p> <ul><li>按<strong>流动方向</strong>：分为输入流和输出流。（写程序时，一定要确定角度）</li> <li>按<strong>数据传输单位</strong>：分为字节流和字符流，即每次传递一个字节（byte）或一个字符（char）
<ul><li>字符和字节是什么关系？</li> <li>一个字符按照编码表可以编码成一个或者多个字节</li></ul></li> <li>按<strong>功能上划分</strong>：分为节点流和处理流，节点流功能单一，处理流功能更强  。</li></ul> <h2 id="四、字节流"><a href="#四、字节流" class="header-anchor">#</a> 四、字节流</h2> <h3 id="_4-1-文件字节输出流-重点"><a href="#_4-1-文件字节输出流-重点" class="header-anchor">#</a> 4.1 文件字节输出流 ( 重点 )</h3> <h4 id="_4-1-1-文件字节输出流介绍"><a href="#_4-1-1-文件字节输出流介绍" class="header-anchor">#</a> 4.1.1 文件字节输出流介绍</h4> <ul><li><h6 id="fileoutputstream类"><a href="#fileoutputstream类" class="header-anchor">#</a> FileOutputStream类 :</h6> <ul><li>OutputStream 字节输出流最顶层的类 , 抽象类</li> <li>FileOutputStream 类是文件字节输出流，用于将数据<strong>写出去</strong>到文件</li></ul></li></ul> <h4 id="_4-1-2-fileoutputstream-成员"><a href="#_4-1-2-fileoutputstream-成员" class="header-anchor">#</a> 4.1.2 FileOutputStream 成员</h4> <ul><li>public FileOutputStream(File file)：创建文件输出流以写入由指定的 File对象表示的文件。</li> <li>void write(int b)  一次写一个字节</li> <li>void write(byte[] buff)  一次写多个字节</li></ul> <p><strong>操作步骤</strong></p> <ul><li><p>1 确定源和目的地</p></li> <li><p>2 创建管道（创建字节输出流对象）</p></li> <li><p>3 把数据写出去</p></li> <li><p>4 释放资源</p></li></ul> <p><strong>需求1：使用文件字节输出流，把程序中数据保存到a.txt文件(先只操作英文)</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">FileOutputStreamDemo</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>

        <span class="token comment">// 1 确定数据源(程序内存)和目的地(a.txt)</span>
        <span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">&quot;hello&quot;</span><span class="token punctuation">;</span>
        <span class="token class-name">File</span> file <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span><span class="token string">&quot;G:\\test\\a.txt&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 2 建立管道（通过具体的IO流对象创建管道）</span>
        <span class="token class-name">FileOutputStream</span> out <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileOutputStream</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 3 把数据写出去</span>
        <span class="token comment">// 写到文件中就是以字节形式存在的，只是文件帮我们把字节翻译成了对应的字符 , 方便查看</span>
        <span class="token comment">/*out.write(97);
        out.write(98);
        out.write(99);*/</span>

        <span class="token comment">// 一次写入多个字节</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> buf <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token string">&quot;UTF-8&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        out<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>buf<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 4 释放资源</span>
        out<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><h3 id="_4-2、文件字节输入流-重点"><a href="#_4-2、文件字节输入流-重点" class="header-anchor">#</a> 4.2、文件字节输入流 ( 重点 )</h3> <h4 id="_4-2-1-字节输入流介绍"><a href="#_4-2-1-字节输入流介绍" class="header-anchor">#</a> 4.2.1 字节输入流介绍</h4> <ul><li>InputStream类 : 字节输入流最顶层的类 , 抽象类</li> <li>FileInputStream类 : 文件字节输入流，专门用于读文件</li></ul> <h4 id="_4-2-2-fileinputstream-类成员"><a href="#_4-2-2-fileinputstream-类成员" class="header-anchor">#</a> 4.2.2 FileInputStream 类成员</h4> <ul><li>public FileInputStream(File file) :  从file类型的路径中读取数据</li> <li>int read() 一次读一个字节，读取到末尾返回-1</li> <li>int read(byte[] buff) 一次读多个字节
<ul><li>从输入流读取最多b.length个字节的数据</li> <li>返回的是真实读到的数据个数</li></ul></li></ul> <p><strong>操作步骤</strong></p> <ul><li>1 确定源和目的地</li> <li>2 创建管道（创建文件字节输入流对象）</li> <li>3 把数据读进来</li> <li>4 释放资源</li></ul> <p><strong>需求1：使用文件字节输入流，读取b.txt文件中的数据</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">FileInputStreamDemo</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
		<span class="token comment">// 1 确定源(a.txt)和目的地(程序内存)</span>
        <span class="token class-name">File</span> file <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span><span class="token string">&quot;G:\\test\\b.txt&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 2 建立输入流管道( 创建具体的IO流对象 )</span>
        <span class="token class-name">FileInputStream</span> in <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span>
        
        <span class="token comment">// 3 把数据读进来:一次读取多个字节</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> buf <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span> 	<span class="token comment">// 临时数组</span>
        <span class="token keyword">int</span> len<span class="token punctuation">;</span> 					<span class="token comment">// 表示读取的有效字节的个数</span>
        
        <span class="token comment">/** 过程解析: 
         * 1 buff和len用于存储读进来的字节和有效个数
         * 2 len=in.read(buff) 读进来多个字节存入buff中，有效个数存入len中
         * 3 把buff中有效的字节，解码成字符，new String(buff,0,len,&quot;UTf-8&quot;)
         */</span>
        <span class="token keyword">while</span><span class="token punctuation">(</span> <span class="token punctuation">(</span>len <span class="token operator">=</span> in<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>buf<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
             <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>buf <span class="token punctuation">,</span> <span class="token number">0</span> <span class="token punctuation">,</span> len<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">// step 4: 关闭管道</span>
        in<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><h2 id="五、字符流"><a href="#五、字符流" class="header-anchor">#</a> 五、字符流</h2> <h3 id="_5-1-文件字符输出流"><a href="#_5-1-文件字符输出流" class="header-anchor">#</a> 5.1 文件字符输出流</h3> <h4 id="_5-1-1-字符输出流介绍"><a href="#_5-1-1-字符输出流介绍" class="header-anchor">#</a> 5.1.1 字符输出流介绍</h4> <ul><li><h6 id="filewriter类"><a href="#filewriter类" class="header-anchor">#</a> FileWriter类 :</h6> <ul><li>Writer 有很多子类，我们从最简单的一个子类开始。</li> <li>FileWriter 类是文件字符输出流，用来写入字符文件的便捷类</li></ul></li></ul> <h4 id="_5-1-2-filewriter-成员"><a href="#_5-1-2-filewriter-成员" class="header-anchor">#</a> 5.1.2 FileWriter 成员</h4> <ul><li>public FileWriter(File file)：创建文件输出流以写入由指定的 File对象表示的文件。</li> <li>void write(int c) 一次写一个字符</li> <li>void write(char[] cbuf) 一次写多个字符</li> <li>void write(String str)  一次写一个字符串</li></ul> <p><strong>操作步骤</strong></p> <ul><li>确定源和目的地</li> <li>创建管道（创建字符输出流对象）</li> <li>把数据写出去</li> <li>释放资源</li></ul> <p><strong>需求1：使用文件字符输出流，把程序中数据保存到c.txt文件，操作中文</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">FileWriterDemo</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>

        <span class="token comment">// 1 确定源和目的地</span>
        <span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">&quot;abc中&quot;</span><span class="token punctuation">;</span>
        <span class="token class-name">File</span> file <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span><span class="token string">&quot;G:\\test\\c.txt&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        
        <span class="token comment">// 2 创建管道</span>
        <span class="token class-name">FileWriter</span> writer <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileWriter</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span>
        
        <span class="token comment">// 3 把数据写出去:一次写一个字符</span>
        <span class="token comment">/*
        writer.write('a');
        writer.write('b');
        writer.write('c');
        writer.write('中');
        */</span>

        <span class="token comment">// 一次写多个字符</span>
        <span class="token comment">/*
        char[] cbuf = str.toCharArray();
        writer.write(cbuf);
        */</span>
		
        <span class="token comment">// writer.write(str);</span>

        <span class="token comment">// 4 释放资源</span>
        writer<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br></div></div><p>注意：FileWriter 写入文件时，以平台默认字符集写入文本型文件。</p> <h3 id="_5-2-文件字符输入流"><a href="#_5-2-文件字符输入流" class="header-anchor">#</a> 5.2 文件字符输入流</h3> <h4 id="_5-2-1-字符输入流介绍"><a href="#_5-2-1-字符输入流介绍" class="header-anchor">#</a> 5.2.1 字符输入流介绍</h4> <ul><li>FileReader  类 :
<ul><li>Reader 读取字符流的最顶层的类 , 抽象类</li> <li>FileReader  是文件字符输入流 : 用来读取字符文件的便捷类</li></ul></li></ul> <h4 id="_5-2-2-filereader的成员"><a href="#_5-2-2-filereader的成员" class="header-anchor">#</a> 5.2.2  FileReader的成员</h4> <ul><li>public FileReader(File file) : 从指定的File路径中读取数据</li> <li>int read() 一次读一个字符数据，读到末尾返回-1</li> <li>int read(char[] cbuf)  一次读一个字符存入cbuf数组
<ul><li>从输入流读取最多cbuf.length个字符的数据</li> <li>返回的是真实读到的字符个数</li></ul></li></ul> <p><strong>需求1：使用文件字符输入流，读取d.txt文件中的数据(包含中文)</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">FileReaderDemo</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
        <span class="token comment">// 1 确定源(c.txt)和目标(内存)</span>
        <span class="token class-name">File</span> file <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span><span class="token string">&quot;F:\\test\\c.txt&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 2 创建管道</span>
        <span class="token class-name">FileReader</span> reader <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileReader</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 3 把数据读进来</span>
        <span class="token comment">// 一次读取多个字符</span>
        <span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span> cbuff <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">char</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> len<span class="token punctuation">;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>len <span class="token operator">=</span> reader<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>cbuff<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>cbuff<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> len<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">// 4 关闭资源</span>
        reader<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><p><strong>字节流和字符流选用问题 ( 面试题 )</strong></p> <ul><li><p>使用记事本打开某个文件，如果可以看到内容的就是文本文件，否则可以暂时认为是二进制格式的。</p></li> <li><p>一般的，<strong>操作二进制文件（图片、音频、视频等）必须使用字节流</strong>。<strong>操作文本文件使用字符流</strong>，尤其是操作带有中文的文件，使用字符流不容易导致乱码，因为使用字节流可能出现读取半个汉字的尴尬（汉字由两个或三个字节组成）。当然，如果不清楚属于哪一类型文件，都可以使用字节流。</p></li></ul> <p>综合应用：把copy_before.txt文件中的数据拷贝到copy_after.txt文件中</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">copy</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
		<span class="token comment">// 1 确定源和目标</span>
        <span class="token class-name">File</span> fromFile <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span><span class="token string">&quot;F:\\test\\copy_before.txt&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">File</span> toFile <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span><span class="token string">&quot;copy_after.txt&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 2 创建输入流和输出流管道</span>
        <span class="token class-name">Reader</span> reader <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileReader</span><span class="token punctuation">(</span>fromFile<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Writer</span> writer <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileWriter</span><span class="token punctuation">(</span>toFile<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 3 具体的IO操作</span>
        <span class="token comment">// 每次读取1024个字符</span>
        <span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span> cbuff <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">char</span><span class="token punctuation">[</span><span class="token number">1024</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> len<span class="token punctuation">;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>len <span class="token operator">=</span> reader<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>cbuff<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            writer<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>cbuff<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span>len<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">// 4 关闭资源</span>
        reader<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        writer<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;复制完成&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><h2 id="六、包装流"><a href="#六、包装流" class="header-anchor">#</a> 六、包装流</h2> <p>前面讲的文件流( FileInputStream/FileOutputStream 、FileReader/FileWriter )属于<strong>节点流</strong>，节点流的功能都比较单一，性能较低。</p> <p>我们能否对节点流进行包装，并提供基于节点流并比它更强大的功能呢？</p> <p><strong>包装流</strong>，相对于节点流更高级，功能更强大，这里存在一个设计模式——装饰者设计模式，此时撇开不谈。</p> <p>包装流如何区分？写代码的时候，发现创建包装流对象时，需要传递另一个流对象，类似：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">new</span>  流类<span class="token class-name">B</span><span class="token punctuation">(</span>  <span class="token keyword">new</span> 流类<span class="token class-name">A</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>那么流B就属于包装流，当然A可能属于节点流也可能属于包装流。</p> <p>有了包装流之后，我们只关心包装流的操作即可，比如只需要关闭包装流即可，无需在关闭节点流。</p> <p>非常重要的包装流——缓冲(Buffer)流，刚才字节流和字符流都有各自的包装流：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>BufferedInputStream / BufferedOutputStream 字节缓冲流
BufferedReader / BufferedWriter 字符缓冲流
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>缓冲流特点：</p> <ul><li>内置了一个默认大小为8192个字节或字符的缓冲区</li> <li>缓冲区的作用用来减少磁盘的IO操作，拿字节缓冲流举例，比如一次性读取8192个字节到内存中，或者存满8192个字节再输出到磁盘中。</li> <li>操作数据量比较大的流，都建议使用上对应的缓冲流。</li></ul> <h3 id="_6-1-字节缓冲流"><a href="#_6-1-字节缓冲流" class="header-anchor">#</a> 6.1 字节缓冲流</h3> <p>需求：把郭德纲-报菜名.mp3文件中的数据拷贝到郭德纲-报菜名2.mp3文件中</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">copy3</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>

        <span class="token class-name">File</span> srcFile <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span><span class="token string">&quot;郭德纲-报菜名.mp3&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">File</span> toFile <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span><span class="token string">&quot;郭德纲-报菜名2.mp3&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 文件输入流管道</span>
        <span class="token class-name">FileInputStream</span> in <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span>srcFile<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">BufferedInputStream</span> bis <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedInputStream</span><span class="token punctuation">(</span>in<span class="token punctuation">,</span><span class="token number">8192</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 文件输出流管道</span>
        <span class="token class-name">FileOutputStream</span> out <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileOutputStream</span><span class="token punctuation">(</span>toFile<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">BufferedOutputStream</span> bos <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedOutputStream</span><span class="token punctuation">(</span>out<span class="token punctuation">,</span><span class="token number">8192</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> buf <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token number">1024</span><span class="token punctuation">]</span><span class="token punctuation">;</span>    <span class="token comment">// 每次可以读取1024个字节(1k)</span>
        <span class="token keyword">int</span> len <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>                   <span class="token comment">// 读取了多少个有效字节</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span> <span class="token punctuation">(</span>len <span class="token operator">=</span> bis<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>buf<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">// 把每次读的1k缓存起来，到8k后自动一次性写出去</span>
            bos<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>buf<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span>len<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        bos<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        bis<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><h3 id="_6-2-字符缓冲流"><a href="#_6-2-字符缓冲流" class="header-anchor">#</a> 6.2 字符缓冲流</h3> <p>BufferedReader 可以对字符流输入流进行包装，提供<strong>对文本型文件进行高效(一次读取一行)的读取</strong>。在BufferedReader 维护了一个缓存区，用于缓存一行数据。提供了读取更强大的方法：</p> <ul><li>readLine() : 一次读取一行，读到末尾返回null</li></ul> <p>BufferedWriter 对字符输出流进行包装 , 可以对<strong>文本型文件进行高效(一次写入一行)的写入</strong>，其中维护了一个缓区，用于缓存一行数据，提供了特有方法</p> <ul><li>newLine() : 写入一个换行。</li></ul> <p>需求: 按行读取f.txt文件</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BufferedReaderDemo</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
        <span class="token comment">/**
         * 分析
         * 1 程序需要读取日志文件(特征:一行一行的数据) =&gt; 读进来 =&gt; 文件字符输入流 =&gt; FileReader
         * 2 FileReader只能一次读一个或多个，不能安行读取，所以需要进行包装 
         * 3 new BufferedReader(FileReader) 得到更强大的流：按行读取
         */</span>
		
    	<span class="token comment">// step 1: 确定数据源</span>
		<span class="token class-name">File</span> file <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span>File</span><span class="token punctuation">(</span><span class="token string">&quot;G:\\test\\g.txt&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		
    	<span class="token comment">// step 2 建立管道</span>
		<span class="token class-name">FileReader</span> in <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileReader</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">BufferedReader</span> reader <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedReader</span><span class="token punctuation">(</span>in<span class="token punctuation">)</span><span class="token punctuation">;</span>
		
    	<span class="token comment">// step 3 操作IO</span>
		<span class="token comment">// 一次读取一行</span>
		<span class="token comment">/*
		String line = null;
		line = reader.readLine();
		line = reader.readLine();
		line = reader.readLine();
		line = reader.readLine();
		
		line = reader.readLine(); // 如果没有可读的行，返回null
		System.out.println(line);
		*/</span>
		
		<span class="token comment">// 循环读取</span>
        <span class="token class-name">String</span> line<span class="token punctuation">;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>line <span class="token operator">=</span> br<span class="token punctuation">.</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
          <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>line<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
		
    	<span class="token comment">// step 4: 关闭流通道</span>
		reader<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br></div></div><p>需求2：把程序中的一首诗按行写入到h.txt文件中</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BufferedWriterDemo</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
        
         <span class="token comment">/**
         * 分析
         * 1 一首诗写入到文件中 =&gt; 写出去 =&gt; 文件字符输出流 =&gt; FileWriter
         * 2 FileWriter不能按行写出去，所以对其进行包装
         * 3 new BufferedWriter(FileWriter) 提供更强大的功能: 按行写出去
         */</span>
        
       <span class="token comment">// step 1: 确定源和目标</span>
        <span class="token class-name">File</span> file <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span><span class="token string">&quot;G:\\test\\f.txt&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// step 2: 建立管道</span>
        <span class="token class-name">FileWriter</span> writer <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileWriter</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span>
         <span class="token comment">// 对writer进行包装，提供更强大的功能: 按行写出去</span>
        <span class="token class-name">BufferedWriter</span> bw <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedWriter</span><span class="token punctuation">(</span>writer<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// step 3: 写入一个字符串后再写入一个换行</span>
        bw<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token string">&quot;床前明月光&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        bw<span class="token punctuation">.</span><span class="token function">newLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        bw<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token string">&quot;疑似地上霜&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        bw<span class="token punctuation">.</span><span class="token function">newLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        bw<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token string">&quot;举头望明月&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        bw<span class="token punctuation">.</span><span class="token function">newLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        bw<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token string">&quot;低头思故乡&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        bw<span class="token punctuation">.</span><span class="token function">newLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// step 4: 关闭通道</span>
        bw<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br></div></div><h2 id="七、小结与作业"><a href="#七、小结与作业" class="header-anchor">#</a> 七、小结与作业</h2> <h3 id="_7-1-小结"><a href="#_7-1-小结" class="header-anchor">#</a> 7.1 小结</h3> <ul><li>了解IO基本概念</li> <li>掌握File文件操作类</li> <li>掌握字符编码与中文乱码问题</li> <li>掌握并区分IO流分类</li> <li>掌握字节流基本操作-FileOutStream + FileInputStream</li> <li>掌握字符流基本操作-FileWriter + FileReader</li> <li>掌握包装流基本操作-BufferedInputStream + BufferedReader</li></ul> <h3 id="_7-2-作业"><a href="#_7-2-作业" class="header-anchor">#</a> 7.2 作业</h3> <h4 id="_7-2-1-题目1"><a href="#_7-2-1-题目1" class="header-anchor">#</a> 7.2.1 题目1</h4> <p>需求：</p> <ul><li>演示File类分隔符和构造器的使用</li> <li>演示File类常用方法</li></ul> <p>作业目的：熟悉File类构造器的使用、常用方法</p> <h4 id="_7-2-2-题目2"><a href="#_7-2-2-题目2" class="header-anchor">#</a> 7.2.2 题目2</h4> <p>需求：字符解码和编码操作</p> <p>作业目的：掌握字符解码和编码操作</p> <h4 id="_7-2-3-题目3"><a href="#_7-2-3-题目3" class="header-anchor">#</a> 7.2.3 题目3</h4> <p>需求：使用文件字节输出流进行输出操作</p> <p>作业目的：掌握输出操作</p> <h4 id="_7-2-4-题目4"><a href="#_7-2-4-题目4" class="header-anchor">#</a> 7.2.4 题目4</h4> <p>需求：使用文件字节输入流进行输入操作</p> <p>作业目的：掌握输入操作</p> <h4 id="_7-2-5-题目5"><a href="#_7-2-5-题目5" class="header-anchor">#</a> 7.2.5 题目5</h4> <p>需求：演示字符文件输入流(FileReader)-输入操作</p> <p>作业目的：掌握字符文件输入流(FileReader)-输入操作</p> <h4 id="_7-2-6-题目6"><a href="#_7-2-6-题目6" class="header-anchor">#</a> 7.2.6 题目6</h4> <p>需求：演示字符文件输出流 (FileWriter)-输出操作</p> <p>作业目的：掌握字符文件输出流 (FileWriter)-输出操作</p> <h4 id="_7-2-7-题目7"><a href="#_7-2-7-题目7" class="header-anchor">#</a> 7.2.7 题目7</h4> <p>需求：使用文件字节流进行文件拷贝</p> <p>作业目的：掌握文件拷贝操作</p> <h4 id="_7-2-8-题目8"><a href="#_7-2-8-题目8" class="header-anchor">#</a> 7.2.8 题目8</h4> <p>1、请根据以下描述完成操作。</p> <p>1）请用IO的知识点，获取文件的内容。文件内容如下：</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub//wps1.jpg" alt="img"></p> <p>2）创建一个Map集合将统计文件D:/e.txt中各个字母出现次数。</p> <p>3）遍历<strong>Map集合</strong>并按照以下格式打印，<strong>格式如下：“A(2),B(5),C(4),D(6),E(3),F(7)”</strong>，括号内代表字符出现次数。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">/**
 * 分析
 * 1 把文件中的内容全部读进来 =&gt; IO =&gt; String str = &quot;ABC...&quot;
 * 2 统计str中每个字符出现的次数 =&gt; Map集合 
 * 3 遍历Map结构即可  =&gt; 遍历方式
 */</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></div></div>  <div class="page-edit"><!----> <div class="tags"><a href="/tags/?tag=IO" title="标签">#IO</a></div> <!----></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/a53030/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">多线程</div></a> <a href="/pages/258730/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">单元测试与配置文件</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/a53030/" class="prev">多线程</a></span> <span class="next"><a href="/pages/258730/">单元测试与配置文件</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/0c07b2/"><div>
            IT杂货铺
            <!----></div></a> <span class="date">03-23</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/705b35/"><div>
            人事百问
            <!----></div></a> <span class="date">03-22</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/78eb56/"><div>
            MyBatis-Plus
            <!----></div></a> <span class="date">03-12</span></dt></dl> <dl><dd></dd> <dt><a href="/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:langfeiyes@163.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/langfeiyes" title="GitHub" target="_blank" class="iconfont icon-github"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2024-2024
    <span><a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备18007927号-1</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
    <script src="/assets/js/app.a2a00aa5.js" defer></script><script src="/assets/js/2.256f807a.js" defer></script><script src="/assets/js/27.1eb62850.js" defer></script>
  </body>
</html>
